package com.mogu.blogs.service.impl;

import com.mogu.blogs.mapper.SortMapper;
import com.mogu.blogs.mapper.TagMapper;
import com.mogu.blogs.pojo.BlogSort;
import com.mogu.blogs.pojo.BlogTitle;
import com.mogu.blogs.service.SortService;
import com.mogu.blogs.service.TagService;
import com.mogu.entity.TBlog;
import com.mogu.entity.TBlogSort;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by 45795 on 2020/6/15.
 */
@Service
public class SortServiceImpl implements SortService {

    @Autowired
    SortMapper sortMapper;
    @Autowired
    TagMapper tagMapper;
//    @Autowired
//    RedisUtil redisUtil;
    /*
    * 查找所有分类
    * */
    @Override
    public List<TBlogSort> findAll() {
        List<TBlogSort> all = sortMapper.findAll();
        return all;
    }
    /*
    * 通过分类ID单查
    * */
    public TBlogSort findById(String uid){
        return sortMapper.findById(uid);
    }
    /*
    * 分类的点击数+1
    * */
    @Override
    public void addClick(String uid) {
        sortMapper.addClick(uid);
    }

    @Override
    public List<BlogTitle> findBlogBySort(String uid) {
        List<TBlog> blogs = sortMapper.findBlogBySort(uid);
        List<BlogTitle> blogTitles = new ArrayList<>();
        for (TBlog blog:blogs){
            BlogTitle blogTitle = new BlogTitle();
            blogTitle.setUid(blog.getUid());
            blogTitle.setAuther(blog.getAuthor());
            blogTitle.setUpdateTime(blog.getUpdateTime());
            blogTitle.setTitle(blog.getTitle());
            //放入分类名字
            blogTitle.setBlogSortName(sortMapper.findById(blog.getBlogSortUid()).getSortName());
            //放入标签名字
            blogTitle.setBlogTagName(tagMapper.findById(blog.getTagUid()).getContent());

            blogTitles.add(blogTitle);
        }
        return blogTitles;
    }

    @Override
    public List<TBlog> findBlogBySortUid(String uid) {
        List<TBlog> blog = this.sortMapper.findBlogBySortUid(uid);
        Collections.reverse(blog);
        return blog;
    }

    /**
     * 通过博客分类uid查询前台分类页面需要的数据
     * @param blogSortUid
     * @return
     */
    @Override
    public List<BlogSort> findBlogSortByBlogSortUid(String blogSortUid) {
        return this.sortMapper.findBlogSortByBlogSortUid(blogSortUid);
    }
}
